package com.leyou.item.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.leyou.common.exceptions.LyException;
import com.leyou.item.dto.SkuDTO;
import com.leyou.item.entity.Sku;
import com.leyou.item.mapper.SkuMapper;
import com.leyou.item.service.SkuService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 虎哥
 */
@Service
public class SkuServiceImpl extends ServiceImpl<SkuMapper, Sku> implements SkuService {
    private static final String UPDATE_STOCK_STATEMENT = "com.leyou.item.mapper.SkuMapper.deductStock";
    private static final String PLUS_STOCK_STATEMENT = "com.leyou.item.mapper.SkuMapper.plusStock";
    @Override
    public List<SkuDTO> querySkuBySpuId(Long spuId) {
        // 1.根据spu查询sku select * from tb_sku where spu_id = #{}
        List<Sku> list = query().eq("spu_id", spuId).list();
        // 2.转换DTO
        return SkuDTO.convertEntityList(list);
    }

    @Override
    @Transactional
    public void deductStock(Map<Long, Integer> skuMap) {
        try {
            executeBatch(sqlSession -> {
                // 遍历sku
                for (Map.Entry<Long, Integer> entry : skuMap.entrySet()) {
                    // 调用mapper中的方法
                    Map<String, Object> param = new HashMap<>();
                    param.put("id", entry.getKey());
                    param.put("num", entry.getValue());
                    // 编译Sql，第一个参数：可以是sql，也可以是statementId：mapper接口名.方法名
                    // 第二个参数是sql需要的参数
                    sqlSession.update(UPDATE_STOCK_STATEMENT, param);
                }
                // 刷新和提交
                sqlSession.flushStatements();
            });
        } catch (Exception e) {
            throw new LyException(400, "库存不足！");
        }

        // 遍历sku
        /*for (Map.Entry<Long, Integer> entry : skuMap.entrySet()) {
            // 调用mapper中的方法
            Map<String, Object> param = new HashMap<>();
            param.put("id", entry.getKey());
            param.put("num", entry.getValue());
            int count = getBaseMapper().deductStock(param);
            if (count == 0) {
                // 更新不成功
                throw new LyException(400, "更新库存失败，可能是已经下架了！");
            }
        }*/
    }

    @Override
    @Transactional
    public void plusStock(Map<Long, Integer> skuMap) {
        try {
            executeBatch(sqlSession -> {
                // 遍历sku
                for (Map.Entry<Long, Integer> entry : skuMap.entrySet()) {
                    // 调用mapper中的方法
                    Map<String, Object> param = new HashMap<>();
                    param.put("id", entry.getKey());
                    param.put("num", entry.getValue());
                    // 编译Sql，第一个参数：可以是sql，也可以是statementId：mapper接口名.方法名
                    // 第二个参数是sql需要的参数
                    sqlSession.update(PLUS_STOCK_STATEMENT, param);
                }
                // 刷新和提交
                sqlSession.flushStatements();
            });
        } catch (Exception e) {
            throw new LyException(400, e.getMessage());
        }
    }
}
